﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Story
{

    public class DialogStateContent : DialogStateBase
    {

        public const float TIME_PER_LETTER = 0.1f;

        private int currentLabelPos = 0;
        private float time = 0f;

        public DialogStateContent(DialogState state, StoryDialogManager controller)
            : base(state, controller)
        {
            
        }

        public override void Enter()
        {
            currentLabelPos = 0;
            time = 0f;

            UpdateHero();

            var dialog = dialogManager.currentDialog;
            if (string.IsNullOrEmpty(dialog.background))
                dialogManager.SetBackGround(string.Empty);
            else
                dialogManager.SetBackGround(dialog.background);
            if (!string.IsNullOrEmpty(dialog.sound))
                dialogManager.PlayAudio(dialog.sound);

        }
        public override void Update()
        {
            var dialog = dialogManager.currentDialog;
            var content = dialog.content;
            if (currentLabelPos > content.Count())
            {
                controller.SwitchState(DialogState.Wait);
                return;
            }
            time += Time.deltaTime;
            if (time > TIME_PER_LETTER)
            {
                if (currentLabelPos > 0 && currentLabelPos - 1 < content.Length && content[currentLabelPos - 1] == '[')
                {
                    if (content[currentLabelPos] == '-')
                    {
                        currentLabelPos += 2;
                    }
                    else
                    {
                        currentLabelPos += 7;
                    }
                }
                if (currentLabelPos >= content.Length)
                {
                    dialogManager.SetText(content);
                }
                else
                {
                    dialogManager.SetText(content.Substring(0, currentLabelPos));
                }
                time = 0;
                currentLabelPos++;
            }
        }

        public override void Click()
        {
            currentLabelPos = dialogManager.currentDialog.content.Length;
        }

        private void UpdateHero()
        {
            var dialog = dialogManager.currentDialog;

            var heroIds = dialog.heroIds_array;
            var faces = dialog.faces_array;
            var heroPos = dialog.heroPos_array;

            string name = dialog.name;
            if(name.Equals("self"))
            {
                name = StaticData.playerData.name;
                if (GameConfig.Instance.GetChannelId == ChannelId.Qihu || GameConfig.Instance.GetChannelId == ChannelId.ios360)
                {
                    name = "结衣";
                }
            }
            dialogManager.SetName(name);

            for (int i=0;i<4;i++)
            {
                string heroId = heroIds[i];
                string face = faces[i];
                double pos = heroPos[i];
                StoryDialogHero hero = dialogManager.heros[i];
                string lastHeroId = hero.GetCurrentDialoyHeroId();
                if (string.IsNullOrEmpty(heroId))
                {
                    hero.gameObject.SetActive(false);
                    continue;
                }

                if (heroId.Equals("self"))
                {
                    if (StaticData.playerData.isPlayerMan)
                        heroId = "self_0";
                    else
                        heroId = "self_1";
                }
                hero.Init(heroId, dialogManager);
                hero.Show();
                hero.SetFocus(false);
                hero.SetFace(face);

                #region UpdatePosition
                float x = 0;
                if (pos >= 0 && pos <= 1)
                    x = dialogManager.container.width * (float)pos;
                if (pos < 0)
                    x = -356f;
                if (pos > 1)
                    x = dialogManager.container.width + 356f;

                if (!lastHeroId.Equals(heroId))
                {
                    hero.transform.localPosition = new Vector3(x, 0, 0);
                }
                else
                {
                    if (Mathf.Abs(x - hero.transform.localPosition.x) > 5f)
                    {
                        TweenActionMove a1 = new TweenActionMove(hero.transform, new Vector3(x, 0, 0), 0.4f, true);
                        TweenActionCurve ae = new TweenActionCurve(a1, dialogManager.curveEaseInOut);
                        dialogManager.tweenComponent.RunAction(ae);
                    }
                }
                #endregion
                
            }
            //if (hero.GetOrder() > order)
            //    hero.SetOrder(hero.GetOrder() - 1);
            //int order = controller.heros[dialog.talker].GetOrder();
            //controller.heros[dialog.talker].SetOrder(4);
            var split = dialog.talker.Split(',');
            var talkers = new int[split.Length];
            for(int i=0;i<split.Length;i++)
            {
                talkers[i] = int.Parse(split[i]);
                dialogManager.heros[talkers[i]].SetOrder(dialogManager.heros[talkers[i]].GetOrder() + 10);
                dialogManager.heros[talkers[i]].SetFocus(true);
            }
            List<int> idxs = new List<int>();
            idxs.Add(0);
            idxs.Add(1);
            idxs.Add(2);
            idxs.Add(3);
            idxs.Sort(CompareOrder);

            for(int i=0;i<idxs.Count;i++)
            {
                dialogManager.heros[idxs[i]].SetOrder(i);
            }
        }
        private int CompareOrder(int a,int b)
        {
            if (dialogManager.heros[a].GetOrder() > dialogManager.heros[b].GetOrder())
                return 1;
            if (dialogManager.heros[a].GetOrder() < dialogManager.heros[b].GetOrder())
                return -1;
            return 0;
        }
    }

}


